package com.example.zk2.frag

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.zk2.entity.BannerEntityResult
import com.example.zk2.entity.CateEntityResult
import com.example.zk2.entity.GoodsEntityResult
import com.example.zk2.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class HomeViewModel:BaseViewModel() {
    val banSuccess = MutableLiveData<BannerEntityResult>()
    val banFail = MutableLiveData<String>()
    fun banner(){
        viewModelScope.launch {
            try {
                val result = apiServer.banner()
                banSuccess.value = result
            }catch (e:Exception){
                banFail.value = e.message
            }


        }
    }

    val cateSuccess = MutableLiveData<CateEntityResult>()
    val cateFail = MutableLiveData<String>()
    fun cate(){
        viewModelScope.launch {
            try {
                val result = apiServer.cate()
                cateSuccess.value = result
            }catch (e:Exception){
                cateFail.value = e.message
            }
        }
    }


    val goodsSuccess = MutableLiveData<GoodsEntityResult>()
    val goodsFail = MutableLiveData<String>()
    fun goods(cateId:Int,page:Int,size:Int){
        viewModelScope.launch{
            try {
                val result = apiServer.goods(cateId, page, size)
                goodsSuccess.value = result
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}